package zpe.jsf.view.bean;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import zpe.genericcrud.jsf.view.bean.AbstractCrudBean;
import zpe.model.Carga;
import zpe.model.Conteiner;
import zpe.model.Enderecamento;
import zpe.model.EnderecamentoHistorico;
import zpe.model.ItemCarga;
import zpe.model.Local;
import zpe.model.enums.Altura;
import zpe.model.enums.Bloco;
import zpe.model.enums.Linha;
import zpe.service.EnderecamentoService;

@ManagedBean(name = "enderecamentoBean")
@ViewScoped
public class EnderecamentoBean extends AbstractCrudBean<Enderecamento, EnderecamentoService> {

	@ManagedProperty("#{enderecamentoService}")
	protected EnderecamentoService service;
	
	private EnderecamentoHistorico enderecamentoHistorico = new EnderecamentoHistorico();
	
	private DataModel<Carga> cargasModal;
	
	private DataModel<EnderecamentoHistorico> enderecamentoDataModel;
	
	private String pesquisaModal;

	@Override
	public void prepareInsert(ActionEvent event) {
		// TODO Auto-generated method stub
		super.prepareInsert(event);
		resetaCampos();
		enderecamentoHistorico.setDataMovimentacao(new Timestamp(new Date().getTime()));
	}
	
	@Override
	public void save(ActionEvent event) {
		// TODO Auto-generated method stub
		enderecamentoHistorico.setEnderecamento(getEntity());
		getEntity().getHistorico().add(enderecamentoHistorico);
		super.save(event);
		resetaCampos();
	}
	
	public void resetaCampos(){
		enderecamentoHistorico = new EnderecamentoHistorico();
		setCargasModal(null);
		enderecamentoDataModel = new ListDataModel<EnderecamentoHistorico>();
	}
	
	@Override
	public void prepareUpdate(ActionEvent event) {
		// TODO Auto-generated method stub
		super.prepareUpdate(event);
		setEntity(getService().findByCarga(getEntity().getCarga()));
		//getEntity().setHistorico(getService().findHistorico(getEntity()));
		enderecamentoHistorico.setDataMovimentacao(new Timestamp(new Date().getTime()));
	}
	
	public void selecionaCarga(ActionEvent ev){
		getEntity().setCarga(getCargasModal().getRowData());
		setCargasModal(null);
		
		setEntity(getService().findByCarga(getEntity().getCarga()));
		enderecamentoDataModel = new ListDataModel<EnderecamentoHistorico>();
		enderecamentoDataModel.setWrappedData(getEntity().getHistorico());

	}
	
	public void findCargas(ActionEvent ev){
		cargasModal = new ListDataModel<Carga>();
	}
	
	public void findCargasByCE(ActionEvent ev){
		cargasModal = new ListDataModel<Carga>();
		Carga model = new Carga();
		model.setNumeroCE(pesquisaModal);
		
		List<Carga> list = new ArrayList<Carga>();
		
		list = getService().getServiceCarga().findByFilter(Carga.class, model.notEmptyFields());
		
		cargasModal.setWrappedData(list);
		
	}
	
	public void adicionarEnderecamento(ActionEvent ev){
		getEntity().getHistorico().add(enderecamentoHistorico);
		getEnderecamentoDataModel().setWrappedData(getEntity().getHistorico());
		enderecamentoHistorico = new EnderecamentoHistorico();
	}
	
	public void editaEnderecamento(ActionEvent ev){
			
	}
	
	public void excluiEnderecamento(ActionEvent ev){
		
	}
	
	public List<SelectItem> getLocais(){
		
		List<SelectItem> itens = new ArrayList<SelectItem>(0);
		
		List<Local> l = getService().getServiceLocal().findAll(Local.class);
		
		if(l!=null && !l.isEmpty()){
			for (Local item : l)
				itens.add(new SelectItem(item, item.getDescricao()));
		}
		
		return itens;
		
	}
	
	public List<SelectItem> getConteiners(){
		
		List<SelectItem> itens = new ArrayList<SelectItem>(0);
		
		if(getEntity().getCarga()!=null && getEntity().getCarga().getId()!=null){
			
			ItemCarga itemCarga = new ItemCarga();
			itemCarga.setCarga(getEntity().getCarga());
		
			List<ItemCarga> l = getService().getServiceItemCarga().findByFilter(ItemCarga.class, itemCarga.notEmptyFields());
			
			if(l!=null && !l.isEmpty()){
				for (ItemCarga item : l){
					if(item.getConteiner()!=null && item.getConteiner().getId()!=null)
						itens.add(new SelectItem(item.getConteiner(), item.getConteiner().toString()));
				}
			}
		
		} 
		return itens;
		
	}
	
	public void selecionaContainer(){
		if(getEntity().getConteiner()!=null && getEntity().getConteiner().getId()!=null){
			getEntity().setQuantidade(new Integer(1));
		} else { 
			getEntity().setQuantidade(new Integer(0));
			getEntity().setConteiner(new Conteiner());
		}
	}
	
	public List<SelectItem> getLinhas() {

		List<SelectItem> itens = new ArrayList<SelectItem>(0);

		for (Linha item : Linha.values())
			itens.add(new SelectItem(item, item.getDescricao()));

		return itens;

	}
	
	public List<SelectItem> getAlturas() {

		List<SelectItem> itens = new ArrayList<SelectItem>(0);

		for (Altura item : Altura.values())
			itens.add(new SelectItem(item, item.getDescricao()));

		return itens;

	}
	
	public List<SelectItem> getBlocos() {

		List<SelectItem> itens = new ArrayList<SelectItem>(0);

		for (Bloco item : Bloco.values())
			itens.add(new SelectItem(item, item.getDescricao()));

		return itens;

	}

	public DataModel<Carga> getCargasModal() {
		return cargasModal;
	}

	public void setCargasModal(DataModel<Carga> cargaModal) {
		this.cargasModal = cargaModal;
	}

	public String getPesquisaModal() {
		return pesquisaModal;
	}

	public void setPesquisaModal(String pesquisaModal) {
		this.pesquisaModal = pesquisaModal;
	}

	public EnderecamentoHistorico getEnderecamentoHistorico() {
		return enderecamentoHistorico;
	}

	public void setEnderecamentoHistorico(
			EnderecamentoHistorico enderecamentoHistorico) {
		this.enderecamentoHistorico = enderecamentoHistorico;
	}

	public DataModel<EnderecamentoHistorico> getEnderecamentoDataModel() {
		return enderecamentoDataModel;
	}

	public void setEnderecamentoDataModel(
			DataModel<EnderecamentoHistorico> enderecamentoDataModel) {
		this.enderecamentoDataModel = enderecamentoDataModel;
	}

}
